﻿/// ********************************
/// 2010.10.29
/// 监控主机信息，包括硬盘空间状况、CPU使用率、内存使用率
/// Author:卢融凯
/// ********************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Lextm.SharpSnmpLib;
using Lextm.SharpSnmpLib.Messaging;
using SManage.LibExtension;
using SManage.Model.Trace;

namespace SManage.Infrastructure
{
    public class HostMonitor
    {
        private IPAddress _ipAddress;

        public HostMonitor(IPAddress ipAddress) {
            if (ipAddress == null) {
                throw new NotImplementedException();
            }

            _ipAddress = ipAddress;
        }

        public IList<StorageItem> GetHostStorageInfo() {
            return GetHostStorageInfo(_ipAddress);
        }

        private static IList<StorageItem> GetHostStorageInfo(IPAddress iPAddress)
        {
            if (iPAddress == null) {
                return null;
            }
            IList<Variable> variables = new List<Variable>();
            Messenger.Walk(
                VersionCode.V2, new IPEndPoint(iPAddress, 161), new OctetString("public"),
                new ObjectIdentifier(HostResource.hrStorage.hrStorageTable.hrStorageTableOid), variables, 
                10000, WalkMode.WithinSubtree);
            var result = new List<StorageItem>();
            int rowCount = variables.Count / 7;
            for (int i = 0; i < rowCount; i++) {
                var item = new StorageItem {
                    hrStorageIndex              = variables[0 * rowCount + i].Data.ToInt32(),
                    hrStorageType               = GetStorageType(variables[1 * rowCount + i]),
                    hrStorageDescr              = variables[2 * rowCount + i].Data.ToString(),
                    hrStorageAllocationUnits    = variables[3 * rowCount + i].Data.ToInt32(),
                    hrStorageSize               = variables[4 * rowCount + i].Data.ToInt32(),
                    hrStorageUsed               = variables[5 * rowCount + i].Data.ToInt32(),
                    hrStorageAllocationFailures = variables[6 * rowCount + i].Data.ToInt32(),
                };
                result.Add(item);
            }
            return result;
        }

        public IList<ProcessorDescription> GetHostProcessorInfo() {
            return GetHostProcessorInfo(_ipAddress);
        }

        public static IList<ProcessorDescription> GetHostProcessorInfo(IPAddress iPAddress)
        {
            if (iPAddress == null) {
                return null;
            }
            IList<Variable> variables = new List<Variable>();
            Messenger.Walk(
                VersionCode.V2, new IPEndPoint(iPAddress, 161), new OctetString("public"),
                new ObjectIdentifier(HostResource.hrDevice.hrProcessorTable.hrProcessorTableOid), variables,
                10000, WalkMode.WithinSubtree);
            var result = new List<ProcessorDescription>();
            int rowCount = variables.Count / 2;
            for (int i = 0; i < rowCount; i++) {
                var item = new ProcessorDescription { 
                    hrProcessorFrwID    = variables[0 * rowCount + i].Data.ToString(),
                    hrProcessorLoad     = variables[1 * rowCount + i].Data.ToInt32()
                };
                result.Add(item);
            }
            return result;
        }
        
        private static string GetStorageType(Variable variable) {
            if (variable == null) {
                return null;
            }
            var str = from type in HostResource.hrStorage.hrStorageTypes
                      where type.Key == variable.Data.ToString()
                      select type.Value.ToString();
            return str.Count() > 0 ? str.First() : null;
        }

        public HostInfo GetHostInfo() {
            var hostInfo = new HostInfo() { 
                ProcessorInfo = GetHostProcessorInfo(),
                StorageInfo = GetHostStorageInfo()
            };

            return hostInfo;
        }
    }
}
